<template>
	<view class="container">
		<view class="content">
			<u-form :model="form" ref="uForm" labelPosition="top">
				<view class="section-title">🔹 基本信息</view>
				<view class="question">
					<u-form-item label="性别" :prop="`singleSex`" labelWidth="auto">
						<u-radio-group placement="column" v-model="form.sex" activeColor="#40d4a8">
							<u-radio :name="1" label="男" :customStyle="{marginBottom: '10px'}"></u-radio>
							<u-radio :name="0" label="女" :customStyle="{marginBottom: '10px'}"></u-radio>
						</u-radio-group>
					</u-form-item>
					
				</view>
				<view class="question">
					<u-form-item label="年龄" :prop="`singleAge`" labelWidth="auto">
						<u--input v-model="form.age" type="number" border="none" placeholder="请输入年龄"></u--input>
					</u-form-item>
				</view>
				<!-- 单选题部分 -->
				<view class="section-title">🔹 单选题（10题）</view>
				<view class="question" v-for="(item, index) in singleChoiceQuestions" :key="index">
					<u-form-item :label="`${index+1}. ${item.question}`" :prop="`single${index}`" labelWidth="auto">
						<u-radio-group placement="column" v-model="form[`single${index}`]" activeColor="#40d4a8">
							<u-radio v-for="(option, optIndex) in item.options" :key="optIndex" :name="option"
								:label="option" :customStyle="{marginBottom: '10px'}"></u-radio>
						</u-radio-group>
					</u-form-item>
				</view>

				<!-- 多选题部分 -->
				<view class="section-title">🔹 多选题（3题）</view>
				<view class="question" v-for="(item, index) in multiChoiceQuestions" :key="index">
					<u-form-item :label="`${index+11}. ${item.question}`" :prop="`multi${index}`" labelWidth="auto">
						<u-checkbox-group placement="column" v-model="form[`multi${index}`]" activeColor="#40d4a8">
							<u-checkbox v-for="(option, optIndex) in item.options" :key="optIndex" :name="option"
								:label="option" :customStyle="{marginBottom: '10px'}"></u-checkbox>
						</u-checkbox-group>
					</u-form-item>
				</view>

				<!-- 填空题部分 -->
				<view class="section-title">🔹 填空题（1题）</view>
				<view class="question">
					<u-form-item label="14. 您对自己目前的健康状况有什么其他想补充的内容？（请简要说明）" prop="comment" labelWidth="auto">
						<u--textarea v-model="form.comment" placeholder="请输入您的补充内容" height="120" count
							border="surround"></u--textarea>
					</u-form-item>
				</view>
			</u-form>

			<view class="submit-btn">
				<u-button color="#40d4a8" type="success" text="提交问卷" shape="circle" @click="submitForm"></u-button>
			</view>
		</view>

		<u-toast ref="uToast"></u-toast>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				userInfo: {},
				form: {
					age: '',
					sex: 1,
					single0: '',
					single1: '',
					single2: '',
					single3: '',
					single4: '',
					single5: '',
					single6: '',
					single7: '',
					single8: '',
					single9: '',
					multi0: [],
					multi1: [],
					multi2: [],
					comment: ''
				},
				singleChoiceQuestions: [{
						question: "您的整体健康状况如何？",
						options: ["非常好", "良好", "一般", "较差"]
					},
					{
						question: "您平均每天的睡眠时间是？",
						options: ["少于5小时", "5-6小时", "7-8小时", "超过8小时"]
					},
					{
						question: "您的日常饮食习惯如何？",
						options: [
							"偏爱油腻、辛辣、高糖食物",
							"偏爱素食或低脂饮食",
							"均衡饮食",
							"经常不规律饮食"
						]
					},
					{
						question: "您每周锻炼的频率是多少？",
						options: ["每天", "每周3-5次", "每周1-2次", "很少或从不锻炼"]
					},
					{
						question: "您是否有慢性疾病（如高血压、糖尿病等）？",
						options: ["是", "否"]
					},
					{
						question: "您的工作或生活压力如何？",
						options: [
							"极大，难以承受",
							"比较大，但可以应对",
							"适中，偶尔有压力",
							"很小，几乎没有"
						]
					},
					{
						question: "您的饮酒习惯如何？",
						options: [
							"经常饮酒（每周3次以上）",
							"偶尔饮酒（每月1-2次）",
							"很少饮酒（每年1-2次）",
							"从不饮酒"
						]
					},
					{
						question: "您的吸烟情况如何？",
						options: ["每天吸烟", "偶尔吸烟", "已戒烟", "从不吸烟"]
					},
					{
						question: "您是否定期进行健康体检？",
						options: ["每年一次或以上", "两年一次", "不定期体检", "从未体检"]
					},
					{
						question: "您的心理状态如何？",
						options: [
							"经常焦虑或抑郁",
							"偶尔情绪低落",
							"心态良好，偶尔有压力",
							"心态积极乐观"
						]
					}
				],
				multiChoiceQuestions: [{
						question: "您平时有哪些健康管理习惯？",
						options: ["定期锻炼", "健康饮食", "规律作息", "定期体检"]
					},
					{
						question: "您是否有以下健康问题？",
						options: ["高血压", "糖尿病", "心脏病", "胃肠疾病"]
					},
					{
						question: "您有哪些缓解压力的方法？",
						options: ["运动", "旅行", "阅读或听音乐", "冥想或放松训练"]
					}
				]
			};
		},
		onShow() {
			this.userInfo = JSON.parse(uni.getStorageSync('userInfo'))
		},
		methods: {
			async submitForm() {
				// 验证必填项
				let isValid = true;
				if(!this.form.age){
					this.$refs.uToast.show({
						message: '请输入年龄',
						type: 'error'
					});
					isValid = false;
					return
				}
				for (let i = 0; i < this.singleChoiceQuestions.length; i++) {
					if (!this.form[`single${i}`]) {
						this.$refs.uToast.show({
							message: `请完成第${i+1}题单选题`,
							type: 'error'
						});
						isValid = false;
						break;
					}
				}

				if (isValid) {
					const AnswerList = []
					this.singleChoiceQuestions.forEach((question, index) => {
						const answer = this.form[`single${index}`]
						const optionIndex = question.options.findIndex(opt => opt === answer)
						AnswerList.push(String.fromCharCode(65 + optionIndex)) // 转A/B/C/D
					})
					this.multiChoiceQuestions.forEach((question, index) => {
						const answers = this.form[`multi${index}`]
						const letters = answers.map(answer => {
							const optionIndex = question.options.findIndex(opt => opt === answer)
							return String.fromCharCode(65 + optionIndex)
						})
						AnswerList.push(letters.sort().join(','))
					})
					const {
						Name,
						OfficerID
					} = this.userInfo
					// 构建最终提交数据
					const submitData = {
						AnswerList: AnswerList.join(','),
						InputAnswer: this.form.comment,
						UserInfo: {
							Age:this.form.age,
							Sex:this.form.sex,
							Name,
							OfficerID
						}
					}
					const {
						data
					} = await uni.$http.post('/submit_health_report', submitData)
					if (data && data.code !== 200) {
						this.$refs.uToast.show({
							message: '提交失败',
							type: 'error'
						});
					} else {
						this.$refs.uToast.show({
							message: '提交成功！',
							type: 'success',
							complete() {
								uni.navigateBack();
							}
						});
					}

				}
			}
		}
	};
</script>
<style lang="scss" scoped>
	.container {
		background-color: #f7f7f7;
		height: 100vh;
		overflow: auto;
	}

	.content {
		padding: 20rpx 30rpx;
		overflow: auto;
	}

	.section-title {
		font-size: 32rpx;
		font-weight: bold;
		color: #333;
		margin: 30rpx 0 20rpx;
	}

	.question {
		background-color: #fff;
		border-radius: 12rpx;
		padding: 20rpx;
		margin-bottom: 30rpx;
		box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.05);

		::v-deep .u-form-item__body__left__content__label {
			font-size: 28rpx !important;
			font-weight: bold;
			color: #333;
			margin-bottom: 20rpx;
		}
	}

	.submit-btn {
		margin: 50rpx 0;
		padding: 0 20rpx;
	}
</style>